Beispiele mit R-code
Faraway - Extending the linear model with r
Faraway - Practical Regression and Anova using R
glmglm()glm() muss 1. ein Formel-Objekt mitgegeben werden und 2. die Klasse (binomial, gaussian, Gamma) samt link-Funktion (logit, probit, cauchit, log, cloglog)install.packages("HSAUR")
library("HSAUR")
data("plasma", package = "HSAUR")
plasma_glm_1 <- glm(ESR ~ fibrinogen, data = plasma,
family = binomial())
install.packages("faraway")
library("faraway")
data(orings)
| temp | damage |
|---|---|
| 53 | 5 |
| 57 | 1 |
| 58 | 1 |
probitmod <- glm(cbind(damage,6-damage) ~ temp,
family=binomial(link=probit), orings)
modp <- glm(Species ~ .,family=poisson,gala)
MASS:library("MASS")
house.plr<-polr(Sat~Infl,weights=Freq,data=housing)
data <- c(1,2,2,3,1,2,3,3,1,2,3,3,1)
fdata <- factor(data)
fdata
## [1] 1 2 2 3 1 2 3 3 1 2 3 3 1
## Levels: 1 2 3
labels direkt definierenrdata <- factor(data,labels=c("I","II","III"))
rdata
## [1] I II II III I II III III I II III III I
## Levels: I II III
levels(fdata) <- c('I','II','III')
fdata
## [1] I II II III I II III III I II III III I
## Levels: I II III
mons <- c("March","April","January",
"November","January","September",
"October","September","November",
"August","January","November",
"November","February","May",
"August","July","December",
"August","August","September",
"November","February","April")
mons <- factor(mons)
table(mons)
## mons
## April August December February January July March
## 2 4 1 2 3 1 1
## May November October September
## 1 5 1 3
mons <- factor(mons,levels=c("January","February",
"March","April","May","June",
"July","August","September",
"October","November",
"December"),
ordered=TRUE)
mons[1] < mons[2]
## [1] TRUE
fert <- c(10,20,20,50,10,20,10,50,20)
fert <- factor(fert,levels=c(10,20,50),ordered=TRUE)
fert
## [1] 10 20 20 50 10 20 10 50 20
## Levels: 10 < 20 < 50
mean(fert)
## [1] NA
mean(as.numeric(levels(fert)[fert]))
## [1] 23.33333
lets <- sample(letters,size=100,replace=TRUE)
lets <- factor(lets)
table(lets[1:5])
##
## a b c d e f g h i j k l m n o p q r s t u v w x y z
## 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0
ggplot2ggplot2<www.r-bloggers.com/basic-introduction-to-ggplot2/>
install.packages("ggplot2")
library(ggplot2)
diamonds Datensatzhead(diamonds)
| carat | cut | color | clarity | depth | table | price | x | y | z |
|---|---|---|---|---|---|---|---|---|---|
| 0.23 | Ideal | E | SI2 | 61.5 | 55 | 326 | 3.95 | 3.98 | 2.43 |
| 0.21 | Premium | E | SI1 | 59.8 | 61 | 326 | 3.89 | 3.84 | 2.31 |
| 0.23 | Good | E | VS1 | 56.9 | 65 | 327 | 4.05 | 4.07 | 2.31 |
| 0.29 | Premium | I | VS2 | 62.4 | 58 | 334 | 4.20 | 4.23 | 2.63 |
| 0.31 | Good | J | SI2 | 63.3 | 58 | 335 | 4.34 | 4.35 | 2.75 |
| 0.24 | Very Good | J | VVS2 | 62.8 | 57 | 336 | 3.94 | 3.96 | 2.48 |
qplotqplot wird für schnelle Graphiken verwendet (quick plots)ggplot kann man alles bis ins Detail kontrollieren# histogram
qplot(depth, data=diamonds)
qplot(cut, depth, data=diamonds)
qplot(factor(cyl), data=mtcars,geom="bar")
qplot(data=diamonds,x=cut,y=depth,geom="boxplot")
# scatterplot
qplot(carat, depth, data=diamonds)
qplot(carat, depth, data=diamonds,color=cut)
myGG<-qplot(data=diamonds,x=carat,y=depth,color=carat)
myGG + stat_smooth(method="lm")
qplot(factor(cyl), data=mtcars, geom="bar") +
coord_flip()
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
Es wird das Paket RColorBrewer verwendet um die Farbpalette zu ändern
install.packages("RColorBrewer")
library(RColorBrewer)
myColors <- brewer.pal(5,"Accent")
names(myColors) <- levels(diamonds$cut)
colScale <- scale_colour_manual(name = "cut",
values = myColors)
p <- ggplot(diamonds,aes(carat, depth,colour = cut)) +
geom_point()
p + colScale
ggsave("Graphik.jpg")
Arten von räumlichen Daten:
Das R-paket ggmap wird im folgenden genutzt um verschiedene Kartentypen darzustellen.
Mit qmap kann man eine schnelle Karte erzeugen.
ggmap:devtools::install_github("dkahle/ggmap")
devtools::install_github("hadley/ggplot2")
install.packages("ggmap")
librarylibrary(ggmap)
Und schon kann die erste Karte erstellt werden:
qmap("Mannheim")
BBT <- qmap("Berlin Brandenburger Tor")
BBT
qmap("Germany")
qmap("Germany", zoom = 6)
?qmap
Verschiedene Abschnitte in der Hilfe:
Ausschnitt aus der Hilfe Seite zum Befehl qmap:
qmap Example
Das Beispiel kann man direkt in die Konsole kopieren:
# qmap("baylor university")
qmap("baylor university", zoom = 14)
# und so weiter
qmap("Mannheim", zoom = 12)
qmap('Mannheim', zoom = 13)
qmap('Mannheim', zoom = 20)
qmap('Mannheim', zoom = 14, maptype="satellite")
qmap('Mannheim', zoom = 20, maptype="hybrid")
qmap("Mannheim", zoom = 14, maptype="hybrid")
Aus Physischen Karten kann man Informationen über Berge, Flüsse und Seen ablesen.
Farben werden oft genutzt um Höhenunterschiede zu visualisieren
qmap('Schriesheim', zoom = 14,maptype="terrain")
New York
Abstraktion wird genutzt um nur die essentiellen Informationen einer Karte zu zeigen.
Bsp. U-Bahn Karten - wichtig sind Richtungen und wenig Infos zur Orientierung
Im folgenden werden Karten vorgestellt, die sich gut als Hintergrundkarten eignen.
qmap('Mannheim', zoom = 14,maptype="watercolor",source="stamen")
qmap('Mannheim', zoom = 14,
maptype="toner",source="stamen")
qmap('Mannheim', zoom = 14,
maptype="toner-lite",source="stamen")
qmap('Mannheim', zoom = 14,
maptype="toner-hybrid",source="stamen")
qmap('Mannheim', zoom = 14,
maptype="terrain-lines",source="stamen")
RstudioExport
<- ist der Zuweisungspfeil um ein Objekt zu erzeugenMA_map <- qmap('Mannheim',
zoom = 14,
maptype="toner",
source="stamen")
Geocoding (…) uses a description of a location, most typically a postal address or place name, to find geographic coordinates from spatial reference data …
library(ggmap)
geocode("Mannheim",source="google")
| lon | lat |
|---|---|
| 8.463243 | 49.48604 |
| cities | lon | lat |
|---|---|---|
| Hamburg | 9.993682 | 53.55108 |
| Koeln | 6.960279 | 50.93753 |
| Dresden | 13.737262 | 51.05041 |
| Muenchen | 11.581981 | 48.13513 |
Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. This permits the identification of nearby street addresses, places, and/or areal subdivisions such as neighbourhoods, county, state, or country.
Quelle: Wikipedia
revgeocode(c(48,8))
## [1] "Unnamed Road, Somalia"
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim")
## from to m km miles seconds minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 749 0.749 0.4654286 212 3.533333
## hours
## 1 0.05888889
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="walking")
## from to m km miles seconds minutes hours
## 1 Q1, 4 Mannheim B2, 1 Mannheim 546 0.546 0.3392844 423 7.05 0.1175
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="bicycling")
## from to m km miles seconds minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 555 0.555 0.344877 215 3.583333
## hours
## 1 0.05972222
POI1 <- geocode("B2, 1 Mannheim",source="google")
POI2 <- geocode("Hbf Mannheim",source="google")
POI3 <- geocode("Mannheim, Friedrichsplatz",source="google")
ListPOI <-rbind(POI1,POI2,POI3)
POI1;POI2;POI3
## lon lat
## 1 8.462844 49.48569
## lon lat
## 1 8.469879 49.47972
## lon lat
## 1 8.475208 49.48326
MA_map +
geom_point(aes(x = lon, y = lat),
data = ListPOI)
MA_map +
geom_point(aes(x = lon, y = lat),col="red",
data = ListPOI)
ListPOI$color <- c("A","B","C")
MA_map +
geom_point(aes(x = lon, y = lat,col=color),
data = ListPOI)
ListPOI$size <- c(10,20,30)
MA_map +
geom_point(aes(x = lon, y = lat,col=color,size=size),
data = ListPOI)
from <- "Mannheim Hbf"
to <- "Mannheim B2 , 1"
route_df <- route(from, to, structure = "route")
qmap("Mannheim Hbf", zoom = 14) +
geom_path(
aes(x = lon, y = lat), colour = "red", size = 1.5,
data = route_df, lineend = "round"
)
Wie fügt man Punkte hinzu
Nutzung von geom_point
Question on stackoverflow
pic
Was klar sein sollte:
visregEin einfaches Modell
N <- 5
x1 <- rnorm(N)
y <- runif(N)
mod1 <- lm(y~x1)
pre <- predict(mod1)
plot(x1,y)
abline(mod1)
segments(x1, y, x1, pre, col="red")
visreg-PaketEin Modell wird auf dem airquality Datensatz geschätzt
install.packages("visreg")
library(visreg)
fit <- lm(Ozone ~ Solar.R + Wind + Temp, data = airquality)
visreg(fit)
visreg visualisiert.visreg(fit, "Wind", type = "contrast")
visregvisreg(fit, "Wind", type = "contrast")
visreg-Paketvisreg(fit, "Wind", type = "conditional")
Mit visreg können die Effekte bei Faktoren visualisiert werden.
airquality$Heat <- cut(airquality$Temp, 3,
labels=c("Cool", "Mild", "Hot"))
fit.heat <- lm(Ozone ~ Solar.R + Wind + Heat,
data = airquality)
par(mfrow=c(1,2))
visreg(fit.heat, "Heat", type = "contrast")
visreg(fit.heat, "Heat", type = "conditional")
airquality$Heat <- cut(airquality$Temp, 3,
labels=c("Cool", "Mild", "Hot"))
fit <- lm(Ozone ~ Solar.R + Wind * Heat, data = airquality)
layoutvisreg(fit, "Wind", by = "Heat",layout=c(3,1))
visreg - Interaktionen overlayfit <- lm(Ozone ~ Solar.R + Wind * Heat, data = airquality)
visreg(fit, "Wind", by="Heat", overlay=TRUE, partial=FALSE)
visreg - visreg2dfit2 <- lm(Ozone ~ Solar.R + Wind * Temp, data = airquality)
visreg2d(fit2, "Wind", "Temp", plot.type = "image")
visreg2d(fit2, "Wind", "Temp", plot.type = "persp")
library("Rcmdr")
Das Paket iplots
install.packages("iplots",dep=TRUE)
library(iplots)
cyl.f <- factor(mtcars$cyl)
gear.f <- factor(mtcars$factor)
attach(mtcars)
ihist(mpg) # histogram
ibar(carb) # barchart
iplot(mpg, wt) # scatter plot
ibox(mtcars[c("qsec","disp","hp")]) # boxplots
ipcp(mtcars[c("mpg","wt","hp")]) # parallel coordinates
imosaic(cyl.f,gear.f) # mosaic plot
library(rggobi)
g <- ggobi(mydata)
attach(mydata)
plot(x, y) # scatterplot
identify(x, y, labels=row.names(mydata)) # identify points
coords <- locator(type="l") # add lines
coords # display list
library(stargazer)
stargazer(attitude)
library(knitr)
kable(head(iris), format = "latex")
pic
Folien zum Workshop:
https://github.com/Japhilko/npRegression/tree/master/slides
library(splines)
interaktiven Karte und Rcode um eine interaktive Karte mit leaflet zu erzeugen.